package com.example.app;

import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.app.retrofit3.api.ApiClient3;
import com.example.app.retrofit3.bean.UpdatePwdResultBean;
import com.example.app.retrofit3.bean.UpdatePwdSendBean;
import com.example.app.util.AuthUtil;
import com.example.app.util.ToastUtil;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UpdatePwdActivity extends AppCompatActivity {
    private static final String TAG = "RegisterTestActivity";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_updatepwd);
        EditText userName = findViewById(R.id.edt_UserName);
        EditText passWord = findViewById(R.id.edt_NewPassWord);
        EditText APP = findViewById(R.id.edt_APP);
        Button updatePwd = findViewById(R.id.btn_Confirm);
        updatePwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int userId = Integer.parseInt(userName.getText().toString());
                String password = passWord.getText().toString();
                String app = APP.getText().toString();
                UpdatePwdSendBean updatePwdSendBean = new UpdatePwdSendBean();
                updatePwdSendBean.setUserId(userId);
                updatePwdSendBean.setPassword(password);
                updatePwdSendBean.setUpdateBy(app);
                Call<UpdatePwdResultBean> call = ApiClient3.getInstance(getApplicationContext()).
                        getReturnData(AuthUtil.GetAuth(),updatePwdSendBean);
                call.enqueue(new Callback<UpdatePwdResultBean>() {
                    @Override
                    public void onResponse(Call<UpdatePwdResultBean> call, Response<UpdatePwdResultBean> response) {
                        if (response.body()!=null){
                            if(response.body().getCode()==200){
                                Log.d(TAG,response.body().getMsg().toString());
                                ToastUtil.ShowToast(getApplicationContext(),"修改密码成功");
                                finish();
                            }else{
                                ToastUtil.ShowToast(getApplicationContext(),response.body().getMsg().toString());
                            }
                        }else {
                            ToastUtil.ShowToast(getApplicationContext(),"网络连接错误");
                        }
                    }

                    @Override
                    public void onFailure(Call<UpdatePwdResultBean> call, Throwable t) {
                        ToastUtil.ShowToast(getApplicationContext(),"网络请求错误");
                    }
                });
            }
        });
    }
}
